package dashboard.utils;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import dashboard.statrpt.field.*;

/**
 * 
 * @author xu guoneng
 *
 */
public class QueryXML {

	DocumentBuilderFactory factory;
	DocumentBuilder builder;
	Document doc = null;
	
	XPathFactory xFactory = XPathFactory.newInstance();
	XPath xpath = xFactory.newXPath();
	XPathExpression expr;
	
	public QueryXML(String filePath) throws ParserConfigurationException, SAXException, IOException{
		factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		builder = factory.newDocumentBuilder();
		doc = builder.parse(filePath);
	}
	
	public void populate(TransferMap map, RptField[] fields) throws Exception{
		for(RptField f : fields){
			Object o = parseObject(f);
			map.put(f, o);
			
			System.out.println(f.getLabel() + o.toString());
		}
		System.out.println("--------------------------------------------------------------");
	}
	
	private String constructXPath(RptField field){
		return "//tr[td='" + field.getFieldName() + "']/td[" + field.getColIndex() + "]/text()";
	}
	
	private Object parseObject(RptField field) throws Exception{
		if(field.getClass().equals(Double.class)){
			return parseDouble(constructXPath(field));
		}
		else{
			return parseString(constructXPath(field));
		}
	}
	
	private String parseString(String path) throws Exception{
		XPathExpression expr = xpath.compile(path);
		Object o = expr.evaluate(doc, XPathConstants.STRING);
		return o == null ? null : ((String)o).trim();
	}
	
	private Double parseDouble(String path) throws Exception{
		XPathExpression expr = xpath.compile(path);
		Object o = expr.evaluate(doc, XPathConstants.NUMBER);
		return o == null ? null : ((Double)o);
	}

	public static void main(String[] args) throws Exception {
		//TODO please set the path of the file correctly
		QueryXML process = new QueryXML("D:\\workspace\\dashboard\\src\\dashboard\\statrpt\\field\\awrrpt_1_77_78.html");

		process.queryCacheSizes();
		process.queryInstanceEfficiencyPercentages();
		process.querySharedPoolStatistics();
	}
	
	//Report Summary - Cache Sizes
	private void queryCacheSizes() throws Exception {
		for(RptField field : RptSumCacheSizes.values()){
			System.out.println(field.getLabel() + parseString(constructXPath(field)));
		}
	}
	
	//Report Summary - Instance Efficiency Percentages
	private void queryInstanceEfficiencyPercentages() throws Exception {
		for(RptField field : RptSumInstanceEfficiencyPercentages.values()){
			System.out.println(field.getLabel() + parseDouble(constructXPath(field)));
		}
	}
	
	//Report Summary - Shared Pool Statistics
	private void querySharedPoolStatistics() throws Exception {
		for(RptField field : RptSumSharedPoolStatistics.values()){
			System.out.println(field.getLabel() + parseDouble(constructXPath(field)));
		}
	}

}
